package cn.datawin.collection;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class TestStream {

    public static void main(String[] args) {
        List<TestStreamEnttiry> dfsfs= new ArrayList<>();
        TestStreamEnttiry testStreamEnttiry = new TestStreamEnttiry();
        TestStreamEnttiry testStreamEnttiry1 = new TestStreamEnttiry();
        TestStreamEnttiry testStreamEnttiry2 = new TestStreamEnttiry();
        TestStreamEnttiry testStreamEnttiry3 = new TestStreamEnttiry();
        TestStreamEnttiry testStreamEnttiry4 = new TestStreamEnttiry();
        testStreamEnttiry.setGroupCode("OCR01");
        testStreamEnttiry.setAmt(new BigDecimal("0.2"));
        testStreamEnttiry.setAmt1(new BigDecimal("0.21"));

        testStreamEnttiry1.setGroupCode("OCR01");
        testStreamEnttiry1.setAmt(new BigDecimal("0.7"));
        testStreamEnttiry1.setAmt1(new BigDecimal("0.71"));

        testStreamEnttiry2.setGroupCode("OCR02");
        testStreamEnttiry2.setAmt(new BigDecimal("0.6"));
        testStreamEnttiry2.setAmt1(new BigDecimal("0.61"));

        testStreamEnttiry3.setGroupCode("OCR02");
        testStreamEnttiry3.setAmt(new BigDecimal("0.2"));
        testStreamEnttiry3.setAmt1(new BigDecimal("0.21"));

        testStreamEnttiry4.setGroupCode("OCR03");
        testStreamEnttiry4.setAmt(new BigDecimal("0.5"));
        testStreamEnttiry4.setAmt1(new BigDecimal("0.51"));

        dfsfs.add(testStreamEnttiry);
        dfsfs.add(testStreamEnttiry1);
        dfsfs.add(testStreamEnttiry2);
        dfsfs.add(testStreamEnttiry3);
        dfsfs.add(testStreamEnttiry4);

        Map<String, List<TestStreamEnttiry>> map = dfsfs.stream()
                .collect(Collectors.groupingBy(TestStreamEnttiry::getGroupCode));
        Map<String, TestStreamEnttiry> countMap =  new HashMap<>();
        for (Map.Entry<String, List<TestStreamEnttiry>> entry : map.entrySet()) {
            String key = entry.getKey();
            List<TestStreamEnttiry> value = entry.getValue();
            TestStreamEnttiry a = value.stream().reduce((x, y) -> {
                BigDecimal amt = x.getAmt().add(y.getAmt());
                BigDecimal amt1 = x.getAmt1().add(y.getAmt1());
                x.setAmt(amt);
                x.setAmt1(amt1);
                return x;
            }).get();
            countMap.put(key,a);
        }
        /**
         *
         Collectors.reducing(new HashMap<>(), enttiry -> {
         Map<String, BigDecimal> valueMap = new HashMap<>();
         valueMap.put("amt", enttiry.getAmt());
         valueMap.put("amt1", enttiry.getAmt1());
         return valueMap;
         }, (map1, map2) -> {
         System.out.println(map1+"----"+map2);
         map1.put("amt", map1.getOrDefault("amt", new BigDecimal("0")) .add( map2.get("amt")));
         map1.put("amt1", map1.getOrDefault("amt1", new BigDecimal("0")) .add( map2.get("amt1")));
         return map1;
         })
         */

        System.out.println(countMap);

    }

}
